package com.superm.controller;

import com.superm.entity.*;
import com.superm.service.BrandService;
import com.superm.service.GoodsService;
import com.superm.service.RelationShipService;
import com.superm.service.StorgageRackService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private RelationShipService relationShipService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private BrandService brandService;
    @Autowired
    private StorgageRackService storgageRackService;
    /**
     * 跳转到商品页面
     * @return
     */
    @RequestMapping("/skipGoods")
    public ModelAndView skipGoods(){
        ModelAndView mv = new ModelAndView("goods/goodsList");
        //查询全部的大类型
        List<RelationShip> relationShipList = relationShipService.selectPidMaxRelationShip();
        //查询全部品牌
        List<Brand> listBrand = brandService.selectLikeBrand(null,null,null);
        mv.addObject("listBrand",listBrand);
        mv.addObject("relationShipList",relationShipList);
        return mv;
    }

    /**
     * 跳转到添加商品页面
     * @return
     */
    @RequestMapping("/skipAddGoods")
    public ModelAndView skipAddGoods(){
        ModelAndView mv = new ModelAndView("goods/addGoods");
        //查询所有品牌
        List<Brand> listBrand = brandService.selectLikeBrand(null,null,null);
        //查询所有货架
        List<StorgageRack> storgageRacksList = storgageRackService.selectStorgageRack(null,null,null);
        //查询所有一级分类
        List<RelationShip> relationShipList = relationShipService.selectPidMaxRelationShip();
        mv.addObject("storgageRacksList",storgageRacksList);
        mv.addObject("relationShipList",relationShipList);
        mv.addObject("listBrand",listBrand);
        return mv;
    }
    /**
     * 模糊查询商品信息
     * @param goodsPoJo
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping("/selectLikeGoods")
    @ResponseBody
    public Map<String,Object> selectLikeGoods(GoodsPoJo goodsPoJo, @RequestParam("page")Integer pageNo,@RequestParam("limit")Integer pageSize){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",0);
        map.put("success",true);
        //查询商品信息
        List<Goods> goodsList =  goodsService.selectLikeGoods(goodsPoJo,pageNo,pageSize);
        //获取总条数
        int count = goodsService.selectGoodsCount(goodsPoJo);
        map.put("data",goodsList);
        map.put("count",count);
        return map;
    }


    /**
     * 添加商品信息
     * @param file
     * @return
     */
    @RequestMapping("/addGoods")
    @ResponseBody
    public Map<String,Object> addGoods(@RequestParam("file") MultipartFile file, GoodsPoJo goodsPoJo, HttpServletRequest request) throws IOException {
        System.out.println(goodsPoJo);
        //先判断货架是否满了，满了就返回提示货架满了,没有就进行文件上传添加
        Map<String,Object> map =  goodsService.addGoods(file,goodsPoJo,request);
        return map;
    }


    /**
     * 跳转到修改商品页面
     * @return
     */
    @RequestMapping("/skipUpdateGoods")
    public ModelAndView skipUpdateGoods(@RequestParam("goodsId") Integer goodsId){
        ModelAndView mv = new ModelAndView("goods/updateGoods");
        //根据商品编号查询指定商品信息
        Goods goods =  goodsService.selectIdGoods(goodsId);
        //查询所有品牌
        List<Brand> listBrand = brandService.selectLikeBrand(null,null,null);
        //查询所有货架
        List<StorgageRack> storgageRacksList = storgageRackService.selectStorgageRack(null,null,null);
        //查询所有一级分类
        List<RelationShip> relationShipList = relationShipService.selectPidMaxRelationShip();
        mv.addObject("storgageRacksList",storgageRacksList);
        mv.addObject("relationShipList",relationShipList);
        mv.addObject("listBrand",listBrand);
        mv.addObject("goods",goods);
        return mv;
    }



    /**
     * 修改商品信息
     * @param file
     * @return
     */
    @RequestMapping("/updateGoods")
    @ResponseBody
    public Map<String,Object> updateGoods(@RequestParam("file") MultipartFile file, GoodsPoJo goodsPoJo, HttpServletRequest request) throws IOException {
        Map<String,Object> map =  goodsService.updateGoods(file,goodsPoJo,request);
        return map;
    }


    /**
     * 商品上架
     * @return
     */
    @RequestMapping("/goodsOffTheShelf")
    @ResponseBody
    public Map<String,Object> goodsOffTheShelf(@RequestParam("goodsId") Integer goodsId){
        Map<String,Object> map = goodsService.goodsOffTheShelf(goodsId);
        return map;
    }

    /**
     * 查询指定货架下的商品
     * @return
     */
    @RequestMapping("/selectStorgageGoods")
    @ResponseBody
    public Map<String,Object> selectStorgageGoods(@Param("storgageRackId")Integer storgageRackId){
        Map<String,Object> map = goodsService.selectStorgageRackId(storgageRackId);
        return map;
    }


    /**
     * 异步方式查询指定商品的信息
     * @return
     */
    @RequestMapping("ajaxSelectIdGoods")
    @ResponseBody
    public Map<String,Object> ajaxSelectIdGoods(@RequestParam("goodsId") Integer goodsId){
         Map<String,Object> map =   goodsService.ajaxSelectIdGoods(goodsId);
        return  map;
    }


}
